package com.app.configuration_center.handler;

import com.app.public_dependency.exception.GlobalException;
import com.app.public_dependency.response.Result;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.rmi.ServerException;

/**
 * @author zhangzy
 * @description 自定义全局异常处理器
 */
@Slf4j
@ControllerAdvice
@ResponseBody
@RequiredArgsConstructor
public class GlobalExceptionHandler {
    private final RedisTemplate<String, String> redisTemplate;
    /**
     * Jwt token 过期
     */
    @ExceptionHandler(value = ExpiredJwtException.class)
    public Result ExpiredJwtExceptionHandler(ExpiredJwtException e) {
        log.error("发生JWT异常！ msg: -> ", e);
        return Result.error(401, "登录信息过期，请重新登陆");
    }
    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = GlobalException.class)
    public Result ServiceExceptionHandler(GlobalException e) {
        log.error("发生业务异常！ msg: -> ", e);
        return Result.error(500, e.getMessage());
    }

    /**
     * 处理空指针的异常
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result NPExceptionHandler(NullPointerException e) {
        log.error("发生空指针异常！ msg: -> ", e);
        return Result.error(500, e.getMessage());
    }

    /**
     * 服务器异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result serverExceptionHandler(Exception e) {
        log.error("服务器异常！ msg: -> ", e);
        return Result.error(500, e.getMessage());
    }


}
